package com.yxzx.ebs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxzx.common.EduException;
import com.yxzx.common.ResultCode;
import com.yxzx.ebs.entity.EbsChapter;
import com.yxzx.ebs.entity.EbsVideo;
import com.yxzx.ebs.entity.vo.EbsChapterVo;
import com.yxzx.ebs.entity.vo.EbsVideoVo;
import com.yxzx.ebs.mapper.EbsChapterMapper;
import com.yxzx.ebs.service.EbsChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzx.ebs.service.EbsVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author kongfuzi
 * @since 2020-07-31
 */
@Service
public class EbsChapterServiceImpl extends ServiceImpl<EbsChapterMapper, EbsChapter> implements EbsChapterService {

    @Autowired
    private EbsVideoService videoService;
    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EbsChapter> queryWrapper = new QueryWrapper<>();;
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }

    /**
     * 根据课程id查询章节和小节信息
     * @param courseId
     * @return
     */
    @Override
    public List<EbsChapterVo> getChapterAndVedioList(String courseId) {
        List<EbsChapterVo> chapterVoList = new ArrayList<>();//封装最终返回数据

        //1.查询课程的章节信息
        QueryWrapper<EbsChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        chapterWrapper.orderByAsc("sort");
        List<EbsChapter> chapterList = baseMapper.selectList(chapterWrapper);

        //2.查询课程的小节视频信息
        QueryWrapper<EbsVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<EbsVideo> ebsVideoList = videoService.list(videoQueryWrapper);

        //3.把小节信息封装到章节对象
        for (int i=0,count = chapterList.size(); i < count; i++){
            EbsChapter ebsChapter = chapterList.get(i);
            //创建章节Vo对象
            EbsChapterVo chapterVo = new EbsChapterVo();
            BeanUtils.copyProperties(ebsChapter,chapterVo);

            //填充视频vo数据
            List<EbsVideoVo> videoVoList = new ArrayList<>();
            for (int j=0,count2 = ebsVideoList.size(); j<count2; j++ ){
                EbsVideo ebsVideo = ebsVideoList.get(j);
                if (ebsChapter.getId().equals(ebsVideo.getChapterId())){
                    //创建视频vo对象
                    EbsVideoVo videoVo = new EbsVideoVo();
                    BeanUtils.copyProperties(ebsVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);//把小节内容封装到章节对象
            chapterVoList.add(chapterVo);//放到最终返回的集合对象
        }
        return chapterVoList;//将章节集合返回
    }

    @Override
    public boolean removeChapterById(String id) {
        //根据id查询是否存在视频
        boolean b = videoService.existsVideo(id);
        if (b){
            throw new EduException(ResultCode.ERROR,"该章节下存在视频课程，请先删除视频课程.");
        }
        return baseMapper.deleteById(id) > 0;
    }
}
